package io.renren.modules.device.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.ExportUtils;
import io.renren.modules.device.service.BuildingService;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.device.entity.DeviceDataEntity;
import io.renren.modules.device.service.DeviceDataService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 设备接受消息
 *
 * @author lwm
 * @email
 * @date 2019-01-10 17:25:51
 */
@RestController
@RequestMapping("device/devicedata")
public class DeviceDataController {
    @Autowired
    private DeviceDataService deviceDataService;
    @Autowired
    private BuildingService buildingService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("device:devicedata:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = deviceDataService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("device:devicedata:info")
    public R info(@PathVariable("id") Long id) {
        DeviceDataEntity deviceData = deviceDataService.selectById(id);

        return R.ok().put("deviceData", deviceData);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("device:devicedata:save")
    public R save(@RequestBody DeviceDataEntity deviceData) {
        deviceDataService.insert(deviceData);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("device:devicedata:update")
    public R update(@RequestBody DeviceDataEntity deviceData) {
        deviceDataService.updateById(deviceData);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("device:devicedata:delete")
    public R delete(@RequestBody Long[] ids) {
        deviceDataService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }


    /*
    获取建筑区域级联数据
     */
    @ResponseBody
    @RequestMapping("/getBuildingArea")
    public R getBuildingArea(int level, String parentKey) {
        Set<String> data = this.buildingService.getBuildingArea(level, parentKey);
        return R.ok().put("data", data);
    }


    @RequestMapping("/export")
    @ResponseBody
    public R exportDeviceData(@RequestParam Map<String, Object> params, HttpServletRequest request) throws IOException {
        PageUtils page = deviceDataService.queryPage(params);

        String[] titiles = new String[]{"id", "设备ID","IMEI", "日期", "时间", "dis1", "dis2", "倾角1", "倾角2", "数据类型", "原始JSON数据"};
        List<String[]> data = new ArrayList<>();
        for (DeviceDataEntity o : (List<DeviceDataEntity>) page.getList()) {
            String date = DateUtils.format(o.getDataDate(), "yyyy-MM-dd");
            String time = DateUtils.format(o.getDataDate(), "HH:mm:ss");
            data.add(new String[]{String.valueOf(o.getId()), o.getDeviceId(),o.getBtImei(), date, time, o.getDis1(), o.getDis2(), o.getQ1(), o.getQ2(), o.getDataType(), o.getJsonData()});

        }
        String fileName = ExportUtils.exportExcel(Arrays.asList(titiles), data);
        String downloadName=System.currentTimeMillis()+".xls";
        String downloadFile=request.getServletContext().getRealPath("/")+downloadName;
        FileUtils.copyFile(new File(fileName),new File(downloadFile));
        return R.ok().put("file",downloadName);
    }

}
